﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Security.Permissions;


namespace Marmat.Forms
{
    [Editor("Marmat.Forms.BarItemCollectionUITypeEditor", typeof(UITypeEditor))]
    [UIPermission(SecurityAction.InheritanceDemand, Window = UIPermissionWindow.AllWindows)]
    public class BarItemCollection : IList
    {
        private BarItem OwnerItem;
        private List<BarItem> List = new List<BarItem>();
        internal DevExpress.XtraBars.BarManager BarManager;
        internal DevExpress.XtraBars.LinksInfo LinkPersistInfo;

        internal BarItemCollection(DevExpress.XtraBars.BarManager BarManager, DevExpress.XtraBars.LinksInfo LinkPersistInfo, BarItem Owner)
        {
            this.OwnerItem = Owner;
            this.BarManager = BarManager;
            this.LinkPersistInfo = LinkPersistInfo;
        }

        public void CopyTo(Array array, int index)
        {
            this.List.CopyTo((BarItem[])array, index);
        }

        public int Count
        {
            get
            {
                return this.List.Count;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return true;
            }
        }

        public object SyncRoot
        {
            get
            {
                return true;
            }
        }

        public IEnumerator GetEnumerator()
        {
            return this.List.GetEnumerator();
        }

        public int Add(BarItem value)
        {
            IList list = this;
            return list.Add(value);
        }

        int IList.Add(object value)
        {
            try
            {
                BarItem Item = (BarItem)value;
                Item._OwnerItem = this.OwnerItem;
                this.List.Add(Item);
                if (this.BarManager is object)
                {
                    this.BarManager.Items.Add(Item.barItem);
                    if (Item is BarSubItem)
                    {
                        foreach (BarItem SubItem in ((BarSubItem)Item).Items)
                        {
                            if (!this.BarManager.Items.Contains(SubItem.barItem))
                            {
                                this.BarManager.Items.Add(SubItem.barItem);
                            }
                        }
                    }
                }

                this.LinkPersistInfo.Add(Item.LinkPersistInfo);
                Item.barItem.Caption = Item.Caption;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }

            return default;
        }

        public void Clear()
        {
            // If Me.BarManager.Form IsNot Nothing AndAlso Me.BarManager.Form.Container IsNot Nothing Then
            // For Each Item As Object In List
            // Me.BarManager.Form.Container.Remove(Item)
            // Next
            // End If
            this.BarManager.Items.Clear();
            this.List.Clear();
        }

        public bool Contains(BarItem value)
        {
            return this.Contains(value);
        }

        bool IList.Contains(object value)
        {
            return this.List.Contains((BarItem)value);
        }

        public bool IndexOf(BarItem value)
        {
            return this.IndexOf(value);
        }

        int IList.IndexOf(object value)
        {
            return this.List.IndexOf((BarItem)value);
        }

        public void Insert(int index, BarItem value)
        {
            this.Insert(index, value);
        }

        void IList.Insert(int index, object value)
        {
            BarItem item = (BarItem)value;
            this.List.Insert(index, item);
            if (!this.BarManager.Items.Contains(item.barItem))
            {
                this.BarManager.Items.Insert(index, item.barItem);
                this.LinkPersistInfo.Insert(index, item.LinkPersistInfo);
            }
        }

        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public BarItem this[string Name]
        {
            get
            {
                foreach (BarItem BarItem in this)
                {
                    if ((BarItem.Name ?? "") == (Name ?? ""))
                    {
                        return BarItem;
                    }
                }

                return null;
            }
        }

        public BarItem this[int index]
        {
            get
            {
                return this.List[index];
            }
        }

        object IList.this[int index]
        {
            get => this.List[index];
            set => this.List[index] = (BarItem)value;
        }

        public void Remove(BarItem value)
        {
            this.Remove(value);
        }

        void IList.Remove(object value)
        {
            BarItem item = (BarItem)value;
            this.List.Remove(item);
            if (this.BarManager.Items.Contains(item.barItem))
            {
                this.BarManager.Items.Remove(item.barItem);
            }
            // If Me.BarManager.Form IsNot Nothing AndAlso Me.BarManager.Form.Container IsNot Nothing Then
            // Me.BarManager.Form.Container.Remove(value)
            // End If
        }

        public void RemoveAt(int index)
        {
            var Item = this.List[index];
            this.List.RemoveAt(index);
            if (this.BarManager.Items.Contains(Item.barItem))
            {
                this.BarManager.Items.RemoveAt(index);
            }
        }
    }
}